package io.geekidea.springbootplus.system.controller;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.geekidea.springbootplus.system.entity.ParcelCenter;
import io.geekidea.springbootplus.system.entity.School;
import io.geekidea.springbootplus.system.service.ParcelCenterService;
import io.geekidea.springbootplus.system.service.SchoolService;
import lombok.extern.slf4j.Slf4j;
import io.geekidea.springbootplus.system.param.SchoolPageParam;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.system.vo.SchoolQueryVo;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.common.param.IdParam;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.core.validator.groups.Update;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *  控制器
 *
 * @author miko
 * @since 2020-10-23
 */
@Slf4j
@RestController
@RequestMapping("/school")
@Module("system")
@Api(value = "API", tags = {""})
public class SchoolController extends BaseController {

    @Autowired
    private ParcelCenterService parcelCenterService;
    @Autowired
    private SchoolService schoolService;

    /**
     * 添加
     */
    @PostMapping("/add")
    @OperationLog(name = "添加", type = OperationLogType.ADD)
    @ApiOperation(value = "添加", response = ApiResult.class)
    public ApiResult<Boolean> addSchool(@Validated(Add.class) @RequestBody School school) throws Exception {
        boolean flag = schoolService.saveSchool(school);
        return ApiResult.result(flag);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @OperationLog(name = "修改", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改", response = ApiResult.class)
    public ApiResult<Boolean> updateSchool(@Validated(Update.class) @RequestBody School school) throws Exception {
        if(school.getDeleted() == 0){
            ParcelCenter parcelCenter = parcelCenterService
                    .getOne(new LambdaQueryWrapper<ParcelCenter>()
                            .eq(ParcelCenter::getSchoolID, school.getSchoolID())
                            .eq(ParcelCenter::getName,"其它"));
            if(parcelCenter == null){
                parcelCenter = new ParcelCenter();
                parcelCenter.setName("其它");
                parcelCenter.setCreatePersonID(1);
                parcelCenter.setSchoolID(school.getSchoolID());
                parcelCenter.setDeleted(0);
                parcelCenter.setCreateTime(new Date());
                parcelCenterService.save(parcelCenter);
            }
        }
        boolean flag = schoolService.updateSchool(school);
        return ApiResult.result(flag);
    }

    /**
     * 删除
     */
    @PostMapping("/delete/{id}")
    @OperationLog(name = "删除", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除", response = ApiResult.class)
    public ApiResult<Boolean> deleteSchool(@PathVariable("id") Long id) throws Exception {
        boolean flag = schoolService.deleteSchool(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取详情
     */
    @GetMapping("/info/{id}")
    @OperationLog(name = "详情", type = OperationLogType.INFO)
    @ApiOperation(value = "详情", response = SchoolQueryVo.class)
    public ApiResult<SchoolQueryVo> getSchool(@PathVariable("id") Long id) throws Exception {
        SchoolQueryVo schoolQueryVo = schoolService.getSchoolById(id);
        return ApiResult.ok(schoolQueryVo);
    }

    /**
     * 分页列表
     */
    @PostMapping("/getPageList")
    @OperationLog(name = "分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "分页列表", response = SchoolQueryVo.class)
    public ApiResult<Paging<SchoolQueryVo>> getSchoolPageList(@Validated @RequestBody SchoolPageParam schoolPageParam) throws Exception {
        Paging<SchoolQueryVo> paging = schoolService.getSchoolPageList(schoolPageParam);
        return ApiResult.ok(paging);
    }


/**
 * 分页列表
 */
@PostMapping("/getAll")
    @OperationLog(name = "所有列表", type = OperationLogType.PAGE)
@ApiOperation(value = "所有列表", response = School.class)
public ApiResult<List<School>> getAll() throws Exception {
        LambdaQueryWrapper<School> wrapper = new LambdaQueryWrapper<>();
        List<School> list = schoolService.getAllList(wrapper);
        return ApiResult.ok(list);
        }

}
